layui.define(['toast'], function (exports) {
    
    let toast = layui.toast;
    
    /**
     *
     * @param url
     * @param type
     * @param data
     * @param dataType
     * @returns {*|boolean}
     */
    const ajax = function (url, type, data, dataType) {
        if (!url || typeof url !== 'string') {
            toast.error('Request Url Null');
            return false;
        }
        return $.ajax({
            url: url,
            type: type || 'post',
            data: data,
            dateType: dataType || 'json'
        });
    };
    
    /**
     *
     * @param url
     * @param data
     * @param show
     */
    const del = (url, data, show) => {
        return new Promise((resolve, reject) => {
            layer.confirm(lang.dialog.confirm_delete || '请再次确定是否删除?', {
                icon: 3,
                title: lang.dialog.miss_tips || '防误提醒',
                offset: '100px',
                btn: [lang.btn.ok || '确认', lang.btn.cancel || '取消'],
            }, function () {
                ajax(url, 'delete', data, 'json').then(res => {
                    if (show) {
                        toast.response(res);
                    }
                    resolve(res);
                }).catch(res => {
                    reject(res);
                });
            });
        });
        
    };
    
    /**
     *
     * @param url
     * @param data
     * @param show
     */
    const get = (url, data, show) => {
        return new Promise((resolve, reject) => {
            ajax(url, 'get', data, 'json').then(res => {
                if (show) {
                    toast.response(res);
                }
                resolve(res);
            }).catch(res => {
                reject(res);
            });
        });
    };
    
    /**
     * @param url
     * @param data
     * @param show
     */
    const post = (url, data, show) => {
        return new Promise((resolve, reject) => {
            ajax(url, 'post', data, 'json').then(res => {
                if (show) {
                    toast.response(res);
                }
                resolve(res);
            }).catch(res => {
                reject(res);
            });
        });
    };
    
    const http = {
        ajax: ajax,
        get: get,
        post: post,
        del: del
    };
    
    exports('http', http);
});